#ifndef GEOMETRY_H
#define GEOMETRY_H

#include "stdio.h"
#include "face.h"
//#include "myarrayoffloat.h"

#include <QDebug>
#include <QList>
#include <QObject>
#include "QtGui/QOpenGLFunctions"

/** 单一的几何体的对象 */
class Geometry : public QObject
{
public: Geometry() {
        //this->faceArray = {new Geometry_Face(1, 1, 1)};
    }
    //~Geometry();
    virtual void initialize() {
        qDebug() << "GEOMETRY INITIALIZED";
        faces = new QList<Face*>();
        vertices = new QList<Vertex3D*>();
    }
    virtual void resetVariables() {

    }
    virtual void addSingleFace() { //添加一个新面

    }

    virtual QString getType() {
        return type;
    }

    virtual GLfloat *verticesBuffer() { //获取顶点缓冲
        return verticesArray;
    }
    virtual GLfloat *normalsBuffer() { //获取法向量缓冲
        return normalsArray;
    }
    virtual GLfloat *colorsBuffer() { //获取法向量缓冲
        return colorsArray;
    }
    virtual int faceCount() { //获取面部的数量
        return faces->length();
    }

    virtual int verticesCountOfLine() {
        return verticesCount;
    }

    virtual void printVertexBuffer() {
        int verticesCount = faces->length() * 9;
        for (int v = 0; v < verticesCount; v++) {
            qDebug() << "FaceIndex" << v << ":" << verticesArray[v];
        }
    }

    virtual void printVerticesWithinRange(GLfloat *m) {
        int verticesCount = faces->length();
        for (int v = 0; v < verticesCount; v++) {
            Face *face = faces->at(v);
            face->printVerticesOfFaceWithinRange(m);
        }
    }
private:
    GLfloat *verticesArray; //节点数组
    int verticesCount = 0; //节点数组长度

    GLfloat *colorsArray; //颜色数组
    int colorsCount = 0; //颜色数组长度

    GLfloat *normalsArray; //法向量数组
    int normalsCount = 0; //法向量数组长度
protected:
    QString type = "default type of geometry"; //几何体的类型
    QList<Face*> *faces; //面数组
    QList<Vertex3D*> *vertices; //节点数组
    const int value = 12;

    virtual void setType(QString s) {
        type = s;
    }

    //添加单一之平面
    virtual void pushSinglePlane(Vertex3D *pointA, Vertex3D *pointB, Vertex3D *pointC) {
        Face *face = new Face(pointA, pointB, pointC);
        this->faces->push_back(face);
    }

    //添加线的缓冲对象
    virtual void generateBufferOfLine(QList<Vertex3D*> currentLine) {
        verticesCount = currentLine.length();
        verticesArray = new GLfloat[verticesCount * 3];

        for (int i = 0; i < currentLine.length(); i++) {
            Vertex3D *sVertex = currentLine.at(i);
            verticesArray[i * 3 + 0] = sVertex->getX();
            verticesArray[i * 3 + 1] = sVertex->getY();
            verticesArray[i * 3 + 2] = sVertex->getZ();
        }
    }

    //添加几何体的缓冲对象
    virtual void generateBufferOfObject() {
        qDebug() << "the length of faces: " << faces->length();
        verticesCount = faces->length() * 9;
        normalsCount = faces->length() * 9;
        colorsCount = faces->length() * 9;
        verticesArray = new GLfloat[verticesCount];
        normalsArray = new GLfloat[normalsCount];
        colorsArray = new GLfloat[colorsCount];
        for (int fi = 0; fi < faces->length(); fi++) {
            Face *singleFace = faces->at(fi);
            Vector3D *normal = singleFace->getNormal(); //获取法向量

            QMap<QString, Vertex3D*> *singleValue = singleFace->getValues();

            Vertex3D *a = singleValue->take("a");
            Vertex3D *b = singleValue->take("b");
            Vertex3D *c = singleValue->take("c");
            //qDebug() << "I belongs to the fairness";

            verticesArray[fi * 9 + 0] = a->getX();
            verticesArray[fi * 9 + 1] = a->getY();
            verticesArray[fi * 9 + 2] = a->getZ();

            verticesArray[fi * 9 + 3] = b->getX();
            verticesArray[fi * 9 + 4] = b->getY();
            verticesArray[fi * 9 + 5] = b->getZ();

            verticesArray[fi * 9 + 6] = c->getX();
            verticesArray[fi * 9 + 7] = c->getY();
            verticesArray[fi * 9 + 8] = c->getZ();

            normalsArray[fi * 9 + 0] = normal->getDeltaX();
            normalsArray[fi * 9 + 1] = normal->getDeltaY();
            normalsArray[fi * 9 + 2] = normal->getDeltaZ();

            normalsArray[fi * 9 + 3] = normal->getDeltaX();
            normalsArray[fi * 9 + 4] = normal->getDeltaY();
            normalsArray[fi * 9 + 5] = normal->getDeltaZ();

            normalsArray[fi * 9 + 6] = normal->getDeltaX();
            normalsArray[fi * 9 + 7] = normal->getDeltaY();
            normalsArray[fi * 9 + 8] = normal->getDeltaZ();

            colorsArray[fi * 9 + 0] = 1.0f;
            colorsArray[fi * 9 + 1] = 0.0f;
            colorsArray[fi * 9 + 2] = 0.0f;

            colorsArray[fi * 9 + 3] = 0.0f;
            colorsArray[fi * 9 + 4] = 1.0f;
            colorsArray[fi * 9 + 5] = 0.0f;

            colorsArray[fi * 9 + 6] = 0.0f;
            colorsArray[fi * 9 + 7] = 0.0f;
            colorsArray[fi * 9 + 8] = 1.0f;

        }
    }
};

#endif // GEOMETRY_H
